package 贪心;

/**
 * @ClassName _134加油站
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/12/20 20:25
 * Version 1.0
 **/
public class _134加油站 {

    public static void main(String[] args) {

        int[] gas = new int[]{2};
        int[] cost = new int[]{2};
        canCompleteCircuit(gas,cost);
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {//超时！！！
        int sum = 0,count = 0;
        for(int i = 0; i < gas.length; i++){
            if(gas[i] > cost[i]){
                for(int j = i; j < gas.length;){
                    if(count >= gas.length){
                        return i;
                    }
                    sum  = sum + gas[j] - cost[j];
                    count++;
                    j++;
                    if (sum < 0){
                        count = 0;
                        sum = 0;
                        break;
                    }
                    j = j%(gas.length);
                }
            }
        }
        return -1;
    }
}
